package com.softer.chengxin.excel.controller;

import com.alibaba.fastjson.JSONObject;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.NumberEnum;
import com.softer.chengxin.common.util.ExcelUtil;
import com.softer.chengxin.excel.domin.ExportExcel;
import com.softer.chengxin.excel.service.ExcelService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author Gong Zhiyang
 * @date 2019/9/4
 */
@Controller
@RequestMapping("/api/excel")
public class DownloadController {
    @Autowired
    private ExcelService excelService;

    @RequestMapping("/download")
    public void download(HttpServletResponse response, String param, HttpSession session) {
        ExportExcel exportExcel = JSONObject.parseObject(param, ExportExcel.class);
        Page page = new Page();
        page.setLimit(exportExcel.getLimit());
        page.setPage(exportExcel.getPage());
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        if (!StringUtils.isBlank(loginName)) {
            Integer district = exportExcel.getDistrict();
            if (NumberEnum.ZERO.getNum().equals(district)) {
                exportExcel.setDistrict(null);
            }
            ManagerPageResult result = excelService.exportExcel(exportExcel, page, loginName);
            ExcelUtil.exportData(response, exportExcel, result.getJson().getDs());
        }
    }
}
